package com.tianhui.consignortwofei.mvp.ui.activity.audit.vehicle;

import android.os.Bundle;
import android.text.TextUtils;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.interf.ISelectItemValue;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.DictionaryType;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.DictionaryInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.VehicleHeadStrategy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class VehicleHeadAuditActivity extends BaseVehicleAuditActivity {

    @BindView(R.id.activity_vehicle_head_audit_plateNumberInputItemView)
    InputItemView mPlateNumberInputItemView;
    @BindView(R.id.activity_vehicle_head_audit_licenseTypeClickItemView)
    ClickItemView mLicenseTypeClickItemView;

    @BindView(R.id.activity_vehicle_road_audit_colorClickItemView)
    ClickItemView mColorClickItemView;
    private String mHeadImageId;
    private SelectDialog mVehicleDialog;
    private SelectDialog mPlateColorDialog;
    private String mColorType;
    private String mLicenseType;

    @Override
    public int getLayoutId() {
        return R.layout.activity_vehicle_head_audit;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public void changePictureSuccess(PictureInfo pictureInfo) {

        mHeadImageId = pictureInfo.id;
    }

    @Override
    public void showVehicleInfo(VehicleInfo info) {
        mPlateNumberInputItemView.setContent(info.vehiclenum);
        mLicenseTypeClickItemView.setContent(info.vehiclenumtypename);
        mVehicleInfo.vehiclenumtype = info.vehiclenumtype;
        mColorClickItemView.setContent(info.vehiclenumcolortypename);
        mColorType = info.vehiclenumcolortype;
        loadPicture(info.imgcarimage);
        mHeadImageId = info.carimage;
    }

    @Override
    public VehicleInfo getSaveInfo() {

        String licenseTypeName = mLicenseTypeClickItemView.getContent();
        if (TextUtils.isEmpty(licenseTypeName)) {
            showToast("请选择牌照类型");
            return null;
        }

        String color = mColorClickItemView.getContent();
        if (TextUtils.isEmpty(color)) {
            showToast("请选择车牌颜色");
            return null;
        }

        mVehicleInfo.vehiclenumcolortypename = color;

        if (TextUtils.isEmpty(mColorType)) {
            mColorType = "2";
        }
        mVehicleInfo.vehiclenumcolortype = mColorType;

        if (TextUtils.isEmpty(mLicenseType)) {
            mLicenseType = "1";
        }
        mVehicleInfo.vehiclenumtype = mLicenseType;
        mVehicleInfo.carimage = mHeadImageId;
        return mVehicleInfo;
    }

    @Override
    public void nextAudit() {

        VehicleInfo vehicleInfo = getSaveInfo();
        if (vehicleInfo != null) {
            Bundle bundle = new Bundle();
            bundle.putParcelable(KeyValue.KEY_VEHICLE_INFO, vehicleInfo);
            bundle.putInt(KeyValue.KEY_OPERATE,getIntent().getExtras().getInt(KeyValue.KEY_OPERATE));
            openActivityForResult(VehicleStatementAuditActivity.class, bundle);
        }

    }

    //车牌颜色
    @OnClick(R.id.activity_vehicle_road_audit_colorClickItemView)
    public void selectPlateColor() {
        hideKeyboard();
        if (null == mPlateColorDialog) {
            Map<String, String> map = new HashMap<>();
            map.put("pid", "78");
            requestTypeList(DictionaryType.TYPE_PLATE_COLOR, map, true);
        } else {
            mPlateColorDialog.show();
        }
    }

    //车辆类型
    @OnClick(R.id.activity_vehicle_head_audit_licenseTypeClickItemView)
    public void selectVehicleType() {
        hideKeyboard();
        if (null == mVehicleDialog) {
            Map<String, String> map = new HashMap<>();
            map.put("pid", "77");
            requestTypeList(DictionaryType.TYPE_PLATE, map, true);
        } else {
            mVehicleDialog.show();
        }
    }

    @Override
    public void showTypeList(int type, List<DictionaryInfo> list) {
        switch (type) {
            case DictionaryType.TYPE_PLATE:
                mVehicleDialog = new SelectDialog(this);
                showSelectDialog(DictionaryType.TYPE_PLATE, "请选择牌照类型", mVehicleDialog, mLicenseTypeClickItemView, list);
                break;
            case DictionaryType.TYPE_PLATE_COLOR:
                mPlateColorDialog = new SelectDialog(this);
                showSelectDialog(DictionaryType.TYPE_PLATE_COLOR, "请选择车牌颜色", mPlateColorDialog, mColorClickItemView, list);
                break;
        }

    }

    @Override
    public void setTypeInfo(int type, ISelectItemValue iSelectItemValue) {
        switch (type) {
            case DictionaryType.TYPE_PLATE:
                mLicenseType = iSelectItemValue.getSelectItemTextValue();
                break;
            case DictionaryType.TYPE_PLATE_COLOR:
                mColorType = iSelectItemValue.getSelectItemTextValue();
                break;
        }
    }

    @OnClick(R.id.layout_audit_picture_editImageView)
    public void changePicture() {
        VehicleHeadStrategy vehicleHeadStrategy = (VehicleHeadStrategy) pictureStrategyFactory.createStrategy(VehicleHeadStrategy.class);
        pictureContext = new PictureContext(vehicleHeadStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo resultInfo) {
                mHeadImageId = resultInfo.pictureId;
                loadPicture(resultInfo.realFileName);
            }

            @Override
            public void selectPhotoFailed() {

            }
        });
    }

}
